﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

namespace Light_Calendar
{
    /// <summary>
    /// Interaction logic for EventBlock.xaml
    /// </summary>
    public partial class EventBlock : UserControl
    {
        private Light_Calendar.MainWindow mainWindow;

        public DateTime Date { get; set; }
        public CalendarEvent Event { get; set; }

        public EventBlock(DateTime date, CalendarEvent e, bool looseLayout)
        {
            InitializeComponent();

            mainWindow = (Light_Calendar.MainWindow)App.Current.MainWindow;
            Date = date;
            Event = e;
            description.Text = e.Description;
            description.ToolTip = e.Description;
            Background = new LinearGradientBrush(Colors.White, e.Color, 90);
            if (looseLayout)
            {
                description.TextTrimming = TextTrimming.None;
                description.TextWrapping = TextWrapping.Wrap;
                description.Height = Double.NaN;
            }
        }

        public EventBlock(DateTime date, CalendarEvent e) : this(date, e, false) { }

        private void EventBlock_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Focus();
            mainWindow.FocusDate(Date, false);
            if (e.ClickCount == 2) Edit();
            e.Handled = true;
        }

        private void EventBlock_GotFocus(object sender, RoutedEventArgs e)
        {
            BorderBrush = Brushes.Gray;
        }

        private void EventBlock_LostFocus(object sender, RoutedEventArgs e)
        {
            BorderBrush = Brushes.LightGray;
        }

        private void EventBlock_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Delete: Delete(); break;
                case Key.Enter: Edit(); break;
            }
        }

        private void Edit()
        {
            if (!mainWindow.IsEditing) mainWindow.Edit(Event);
        }

        private void Delete()
        {
            mainWindow.IsDeleting = true;
            if (MessageBox.Show("Are you sure you want to delete \"" + Event.Description + "\"?", "Delete Event",
                MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                mainWindow.Data.Remove(Event);
                new Thread(new ThreadStart(mainWindow.Data.Save)).Start();
            }
            mainWindow.IsDeleting = false;
        }

        private void edit_Click(object sender, RoutedEventArgs e)
        {
            Edit();
        }

        private void delete_Click(object sender, RoutedEventArgs e)
        {
            Delete();
        }

        private void new_Click(object sender, RoutedEventArgs e)
        {
            if (!mainWindow.IsEditing) mainWindow.New(Date);
        }

        private void today_Click(object sender, RoutedEventArgs e)
        {
            mainWindow.FocusDate(DateTime.Today);
        }
    }
}
